package com.best.onegous.Adapter;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.best.onegous.R;
import com.best.onegous.agreement.Apps;
import com.best.onegous.agreement.Common;
import com.best.onegous.details.User;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyShaiDanAdapter extends BaseAdapter{
	Intent intent;
	  private List<JSONObject> data = new ArrayList<JSONObject>();  
	    private LayoutInflater layoutInflater;  
	    private Context context;  
	    public MyShaiDanAdapter(Context context, List<JSONObject> data) {  
	        this.context = context;  
	        this.data = data;  
	        this.layoutInflater = LayoutInflater.from(context);  
	    }     
	@Override
	public int getCount() {
		// TODO Auto-generated method stub

		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub

		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}


	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
			ViewHome viewhome=null;
			if(convertView==null){
				viewhome = new ViewHome();
				convertView = layoutInflater.inflate(R.layout.myshaidan_item, null);
				viewhome.userimg = (ImageView) convertView.findViewById(R.id.userimg);
				viewhome.img1 = (ImageView) convertView.findViewById(R.id.img1);
				viewhome.img2 = (ImageView) convertView.findViewById(R.id.img2);
				viewhome.img3 = (ImageView) convertView.findViewById(R.id.img3);
				viewhome.name = (TextView) convertView.findViewById(R.id.name);
				viewhome.id = (TextView) convertView.findViewById(R.id.id);
				viewhome.title = (TextView) convertView.findViewById(R.id.title);
				viewhome.context = (TextView) convertView.findViewById(R.id.context);
				viewhome.dizhi = (TextView) convertView.findViewById(R.id.dizhi);
				viewhome.time = (TextView) convertView.findViewById(R.id.time);
				viewhome.allimg = (LinearLayout) convertView.findViewById(R.id.allimg);
				convertView.setTag(viewhome);
			}else{
				viewhome = (ViewHome) convertView.getTag();
			}
			JSONObject item = data.get(position);
			try {
				JSONArray postAllPic = item.getJSONArray("postAllPic");
				int size = postAllPic.length();
				if(size>0){
					viewhome.allimg.setVisibility(View.VISIBLE);
					for(int i = 0;i<size;i++){
						if(0==i){
							viewhome.img1.setVisibility(View.VISIBLE);
							Apps.imageLoader.displayImage(Common.PHOTOURL+postAllPic.get(i), viewhome.img1, Apps.yuanimg);
						}else if(1==i){
							viewhome.img2.setVisibility(View.VISIBLE);
							Apps.imageLoader.displayImage(Common.PHOTOURL+postAllPic.get(i), viewhome.img2, Apps.yuanimg);
						}
						else if(1==i){
							viewhome.img3.setVisibility(View.VISIBLE);
							Apps.imageLoader.displayImage(Common.PHOTOURL+postAllPic.get(i), viewhome.img3, Apps.yuanimg);
						}
					}
				}
				
				Apps.imageLoader.displayImage(Common.PHOTOURL+item.getString("userPhoto"), viewhome.userimg, Apps.touxiang);
				viewhome.name.setText(item.getString("userName"));
				viewhome.id.setText("ID："+item.getString("userWeb"));
				viewhome.time.setText(item.getString("postTime"));
				viewhome.title.setText(item.getString("postTitle"));
				viewhome.context.setText(item.getString("postContent"));
				viewhome.dizhi.setText(item.getString("address"));
				
				
				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return convertView;
		}
	 class ViewHome{  
		 private ImageView userimg,img1,img2,img3;
		    private TextView name,id,time,title,context,dizhi;
		    private LinearLayout allimg;
		    
		}
		
	}